package com.haitonghop.modules.local;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.haitonghop.R;
import com.haitonghop.utils.Logger;

@SuppressLint("SetJavaScriptEnabled")
public class UtilsWebActivity extends Activity{

	WebView webView;
	LinearLayout loadingProcess;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		String value = "";
		if (extras != null) {
		    value = extras.getString("IMAGE_ID");
		    Logger.logMe("value = "+value);
		}

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.utils_activity_layout);

		webView = (WebView)findViewById(R.id.webViewComment);
		loadingProcess = (LinearLayout)findViewById(R.id.loadingProcessComment);
		loadingProcess.setVisibility(View.VISIBLE);

		String url = "http://haibl.net/public/util";

		if(webView != null){
			webView.setVisibility(View.INVISIBLE);
			webView.loadUrl(url);
			webView.setPadding(0, 0, 0, 0);
			webView.setInitialScale(100);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.setWebViewClient(chromeClient);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if(webView.canGoBack()){
				webView.goBack();
			} else {
				finish();
			}
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}


	WebViewClient chromeClient = new WebViewClient(){

		@Override
		public void onPageFinished(WebView view, String url) {
			loadingProcess.setVisibility(View.GONE);
			webView.setVisibility(View.VISIBLE);
		};
	};
}
